using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Xml;
using System.IO;
using System.Threading;
using System.Collections;
using System.Collections.Specialized;

namespace datagridviewttest
{
    public partial class Form1 : Form
    {

        static byte[] buffer;
        static int size;
        static Socket s;
        static DataSet nodes = new DataSet();
        static DataTable nodetable = nodes.Tables.Add("Node Data");
        static Color stable = Color.Green;
        static Color unstable = Color.Red;
        
        

        delegate void RefreshView();

        private void updateDisplay()
        {
            this.dataGridView1.Refresh();
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            disconnectToolStripMenuItem.Enabled = false;
            DataColumn nodeid = nodetable.Columns.Add("Node ID", typeof(Int32));
            DataColumn[] columns = new DataColumn[1];
            columns[0] = nodetable.Columns["Node ID"];
            nodetable.PrimaryKey = columns;
            
            
                        
            nodetable.Columns.Add("Voltage", typeof(Int32));
            nodetable.Columns.Add("PSI", typeof(Int32));
            nodetable.Columns.Add("ADC1", typeof(Int32));
            nodetable.Columns.Add("ADC2", typeof(Int32));
            nodetable.Columns.Add("ADC3", typeof(Int32));
            nodetable.Columns.Add("DIGI0", typeof(Int32));
            nodetable.Columns.Add("DIGI1", typeof(Int32));
            nodetable.Columns.Add("DIGI2", typeof(Int32));
            nodetable.Columns.Add("DIGI3", typeof(Int32));
            dataGridView1.DataSource = nodetable;
            

            
            
        }

        

        private void DataPolling(IAsyncResult result)
        {
            //now connected to xserve; prepare to receive data
            if (s.Connected)
            {
                while (true)
                {
                    buffer = new byte[4];

                    s.Receive(buffer, 0, 4, SocketFlags.None);
                   
                    size = BitConverter.ToInt32(buffer, 0);
                    
                    if (size == 0)
                    {
                        continue;
                    }
                    buffer = new byte[size];
                    s.Receive(buffer, 0, size, SocketFlags.None);

                    MemoryStream xmlpacket = new MemoryStream(buffer);
                    XmlTextReader xmlreader = new XmlTextReader(xmlpacket);
                    xmlreader.WhitespaceHandling = WhitespaceHandling.All;
                    String temptype;
                    object[] temprow = new object[10];
                    int tempvalue = 0;
                    bool ignore = false;

                    while (xmlreader.Read())
                    {
                        temptype = null;

                        if (xmlreader.NodeType == XmlNodeType.Element && xmlreader.Name == "Name")
                        {
                            xmlreader.Read();
                            if (xmlreader.NodeType == XmlNodeType.Text)
                            {
                                temptype = xmlreader.ReadContentAsString();
                                xmlreader.Read();
                                if (xmlreader.NodeType == XmlNodeType.Element && xmlreader.Name == "ConvertedValue")
                                {
                                    xmlreader.Read();
                                    tempvalue = xmlreader.ReadContentAsInt();

                                }
                            }
                        }
                        if (temptype != null)
                        {
                            switch (temptype)
                            {
                                case "amtype":
                                    if (tempvalue != 11)
                                        ignore = true;

                                    break;
                                case "nodeid":
                                    temprow[0] = tempvalue;
                                    break;
                                case "parent":
                                    break;
                                case "group":
                                    break;
                                case "socketid":
                                    break;
                                case "board_id":
                                    break;
                                case "packet_id":
                                    break;
                                case "voltage":
                                    temprow[1] = tempvalue;
                                    break;
                                case "adc0":
                                    temprow[2] = tempvalue;
                                    break;
                                case "adc1":
                                    temprow[3] = tempvalue;
                                    break;
                                case "adc2":
                                    temprow[4] = tempvalue;
                                    break;
                                case "adc3":
                                    temprow[5] = tempvalue;
                                    break;
                                case "digi0":
                                    temprow[6] = tempvalue;
                                    break;
                                case "digi1":
                                    temprow[7] = tempvalue;
                                    break;
                                case "digi2":
                                    temprow[8] = tempvalue;
                                    break;
                                case "digi3":
                                    temprow[9] = tempvalue;
                                    break;
                            }
                        }
                        if (ignore)
                            break;
                    }
                    //Close the reader.
                    xmlreader.Close();
                    if (ignore)
                        continue;
                    
                    //do comparisons to see if node already exists in table; then either update or add
                    nodetable.BeginLoadData();
                    nodetable.LoadDataRow(temprow, true);
                    nodetable.EndLoadData();
                    
                    RefreshView r = new RefreshView(updateDisplay);
                    this.Invoke(r);
                    
                    
                    
                }

            }

        }
